//
//  VideoPlayViewController.m
//  ZXVideoPlayer
//
//  Created by Shawn on 16/4/29.
//  Copyright © 2016年 Shawn. All rights reserved.
//

#import "DetailVideoViewController.h"
#import "LoginViewController.h"
#import "AppHeader.h"
#import "DynamicTrainVC.h"
#import "RightModel.h"
#import "UINavigationController+FDFullscreenPopGesture.h"

@interface DetailVideoViewController ()<ZFPlayerDelegate>
@property(nonatomic,strong) UIView *playerFatherView;

@property (nonatomic, strong) ZFPlayerView *videoController;

@property (nonatomic, assign) BOOL isPlaying;

@property (nonatomic, strong) ZFPlayerModel *playerModel;

@property (nonatomic, strong) ZFPlayerControlView *controlView;

@property(nonatomic,strong) UIButton *likeBtn;

@property(nonatomic,strong) UIButton *commentBtn;

@property(nonatomic,strong) UILabel *desLabel;

@property(nonatomic,strong) UILabel *timeLable;

@property(nonatomic,strong) UIButton *stopBtn;

@property(nonatomic,strong) UIButton *nextBtn;

@property(nonatomic,strong) UIButton *preBtn;

@property (nonatomic, assign) NSInteger index;

@property(nonatomic,strong) NSArray *arrData;

@property(nonatomic,strong) UILabel *mainLabDes;

@property(nonatomic,strong) UILabel  *likeCountLabel;

@property(nonatomic,strong) UILabel *noticeLabel;

@property(nonatomic,strong) RightModel *model;

@end

@implementation DetailVideoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    
    self.view.backgroundColor = [UIColor ColorWithHex:@"#474747"];
    
    [self playVideo];
    
    [self initUI];
}

-(void)viewWillDisappear:(BOOL)animated{
//    [self.videoController stop];
}

-(void)setMainLabDes:(UILabel *)mainLabDes{
    _mainLabDes = mainLabDes;
}
-(void)setDesLabel:(UILabel *)desLabel{
    _desLabel = desLabel;
}
-(void)setLikeCountLabel:(UILabel *)likeCountLabel{
    _likeCountLabel = likeCountLabel;
}

- (void)playVideo{
    if (!self.videoController) {
        self.playerFatherView = [[UIView alloc] init];
        [self.view addSubview:self.playerFatherView];
        [self.playerFatherView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(20);
            make.leading.trailing.mas_equalTo(0);
            // 这里宽高比16：9,可自定义宽高比
            make.height.mas_equalTo(self.playerFatherView.mas_width).multipliedBy(9.0f/16.0f);
        }];
        self.videoController = [[ZFPlayerView alloc]init];
        [self.playerFatherView addSubview:self.videoController];
        [self.videoController mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view).offset(20);
            make.left.right.equalTo(self.view);
            make.height.equalTo(self.videoController.mas_width).multipliedBy(9.0f/16.0f);
        }];
        self.playerModel = [[ZFPlayerModel alloc]init];
        _playerModel.videoURL = [NSURL URLWithString:self.videoUrl];
        _playerModel.title = self.titles;
        _playerModel.placeholderImage = [UIImage imageNamed:@""];
        _playerModel.fatherView       = self.playerFatherView;
        [self.videoController playerControlView:_controlView playerModel:_playerModel];
        self.videoController.delegate = self;
        [self.videoController autoPlayTheVideo];
}
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    [self LoadLikeCount];
    [self LoadVideoDetialInfo];
    [self.videoController play];
}

// 返回值要必须为NO
- (BOOL)shouldAutorotate
{
    return NO;
}

#pragma mark - ZFPlayerDelegate

- (void)zf_playerBackAction
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (ZFPlayerControlView *)controlView
{
    if (!_controlView) {
        _controlView = [[ZFPlayerControlView alloc] init];
    }
    return _controlView;
}


-(void)initUI{
    
    self.desLabel = [[UILabel alloc]init];
    [self.view addSubview:self.desLabel];
    [self.desLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.playerFatherView.mas_bottom).offset(10);
        make.left.equalTo(self.view).offset(15);
    }];
    self.desLabel.textColor = [UIColor whiteColor];
    self.desLabel.textAlignment = NSTextAlignmentLeft;
    self.desLabel.font = [UIFont systemFontOfSize:17];
    
    self.commentBtn = [[UIButton alloc]init];
    [self.view addSubview:self.commentBtn];
    [self.commentBtn setImage:[UIImage imageNamed:@"review"] forState:UIControlStateNormal];
    [self.commentBtn addTarget:self action:@selector(commentBtnDidClick) forControlEvents:UIControlEventTouchUpInside];
    [self.commentBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view).offset(-20);
        make.centerY.equalTo(self.desLabel.mas_centerY).offset(3);
        make.width.height.equalTo(@30);
    }];

    self.likeBtn = [[UIButton alloc]init];
    [self.view addSubview:self.likeBtn];
    [self.likeBtn setImage:[UIImage imageNamed:@"admire"] forState:UIControlStateNormal];
    [self.likeBtn addTarget:self action:@selector(likeBtnDIidClick) forControlEvents:UIControlEventTouchUpInside];
    [self.likeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.commentBtn.mas_left).offset(-20);
        make.centerY.equalTo(self.desLabel.mas_centerY);
        make.width.height.equalTo(@30);
    }];
    
    self.likeCountLabel = [[UILabel alloc]init];
    [self.view  addSubview:self.likeCountLabel];
    [self.likeCountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.likeBtn.mas_centerX);
        make.top.equalTo(self.likeBtn.mas_bottom);
    }];
    self.likeCountLabel.textColor = [UIColor whiteColor];
    self.likeCountLabel.textAlignment = NSTextAlignmentCenter;
    self.likeCountLabel.font = [UIFont systemFontOfSize:11];
    
    
    UILabel *lineLbl = [[UILabel alloc]init];
    lineLbl.backgroundColor = [UIColor ColorWithHex:@"#616360"];
    [self.view addSubview:lineLbl];
    [lineLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.desLabel.mas_bottom).offset(20);
        make.left.equalTo(self.view).offset(15);
        make.right.equalTo(self.view).offset(-15);
        make.height.equalTo(@2);
    }];
    
    UILabel *mainLabel = [[UILabel alloc]init];
    [self.view addSubview:mainLabel];
    [mainLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lineLbl.mas_bottom).offset(10);
        make.left.equalTo(lineLbl.mas_left);
    }];
    mainLabel.text = @"要点";
    mainLabel.textColor = [UIColor ColorWithHex:@"#a9aaa7"];
    mainLabel.font = [UIFont systemFontOfSize:20];
    
    self.mainLabDes = [[UILabel alloc]init];
    [self.view addSubview:self.mainLabDes];
    [self.mainLabDes mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(mainLabel.mas_bottom);
        make.left.equalTo(self.view).offset(15);
        make.right.equalTo(self.view).offset(-15);
        make.height.equalTo(@90);
    }];
    self.mainLabDes.textColor = [UIColor ColorWithHex:@"#f9faf7"];
    self.mainLabDes.textAlignment = NSTextAlignmentLeft;
    self.mainLabDes.font = [UIFont systemFontOfSize:15];
    self.mainLabDes.numberOfLines =  0;
}

-(void)likeBtnDIidClick{
    
    NSString *userLogin = [CommonMethod fuckNULL:[UserDefaultsUtlis getValueForKey:@"userid"]];
    NSLog(@"----%@",userLogin);
    if (userLogin.length == 0) {
        LoginViewController *vc = [LoginViewController new];
        UINavigationController *navVc = [[UINavigationController alloc]initWithRootViewController:vc];
        [self presentViewController:navVc animated:YES completion:nil];
        return;
    }
    
    
    NSString *url = [BASEURL  stringByAppendingString:kURL_Click];
    NSDictionary *dict = @{@"recoveryId":self.rid};
    NSString *jsonString = [NSString dictionaryToJson:dict];
    NSString *AESString = [NSString AES128Encrypt:jsonString];
    NSDictionary *parameters = @{@"parameters":AESString,
                                 @"login":[NSString  AES128Encrypt:[NSString stringWithFormat:@"%@@@%@",[[NSUserDefaults standardUserDefaults]objectForKey:@"userid"],ApplicationDelegate.deviceID]]};
    NSLog(@"%@",dict);
    NSLog(@"%@",parameters);
    [AppNetwork requestWithPath:url parameters:parameters  success:^(id responseObject) {
        NSLog(@"%@",responseObject);
        NetworkModel *network = [NetworkModel mj_objectWithKeyValues:responseObject];
        NSInteger code = network.code.integerValue;
        if (code == 0){
          [CustomProgressHUD showSuccessWithStatus:@"恭喜您点赞成功!!!"];
          [self LoadLikeCount];
        }else{
            [CustomProgressHUD showErrorWithStatus:network.errorDescription];
        }
    } failure:^(id error) {
        [CustomProgressHUD showErrorWithStatus:k_NetWorkStatus_Error];
    }];
}
-(void)commentBtnDidClick{
    
    NSString *userLogin = [CommonMethod fuckNULL:[UserDefaultsUtlis getValueForKey:@"userid"]];
    NSLog(@"----%@",userLogin);
    if (userLogin.length == 0) {
        LoginViewController *vc = [LoginViewController new];
        UINavigationController *navVc = [[UINavigationController alloc]initWithRootViewController:vc];
        [self presentViewController:navVc animated:YES completion:nil];
        return;
    }
    
    [self.videoController pause];
    DynamicTrainVC *vc = [DynamicTrainVC new];
    vc.recoveryId = self.model.id;
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)LoadVideoDetialInfo{
    
    NSString *url = [BASEURL stringByAppendingString:kURL_recovery];
    NSDictionary *dict = @{@"rid":self.rid};
    NSString *jsonString = [NSString dictionaryToJson:dict];
    NSString *AESString = [NSString AES128Encrypt:jsonString];
    NSDictionary *parameters = @{@"parameters":AESString};
    NSLog(@"%@",dict);
    [AppNetwork requestWithPath:url parameters:parameters  success:^(id responseObject) {
        NSLog(@"%@",responseObject);
        NetworkModel *network = [NetworkModel mj_objectWithKeyValues:responseObject];
        NSInteger code = network.code.integerValue;
        if (code == 0){
            RightModel *model = [RightModel mj_objectWithKeyValues:network.dataObject];
            self.model = model;
            self.mainLabDes.text = model.miaoShu;
            self.desLabel.text = model.title;
        }else{
            [CustomProgressHUD showErrorWithStatus:network.errorDescription];
        }
    } failure:^(id error) {
        [CustomProgressHUD showErrorWithStatus:k_NetWorkStatus_Error];
    }];
}

-(void)LoadLikeCount{
    
    NSString *userLogin = [CommonMethod fuckNULL:[UserDefaultsUtlis getValueForKey:@"userid"]];
    NSLog(@"----%@",userLogin);
    if (userLogin.length == 0) {
        return;
    }
    
    NSString *url = [BASEURL  stringByAppendingString:kURL_Count];
    NSDictionary *dict = @{@"recoveryId":self.rid};
    NSString *jsonString = [NSString dictionaryToJson:dict];
    NSString *AESString = [NSString AES128Encrypt:jsonString];
    NSDictionary *parameters = @{@"parameters":AESString,
                                 @"login":[NSString  AES128Encrypt:[NSString stringWithFormat:@"%@@@%@",[[NSUserDefaults standardUserDefaults]objectForKey:@"userid"],ApplicationDelegate.deviceID]]};
    NSLog(@"%@",dict);
    [AppNetwork requestWithPath:url parameters:parameters  success:^(id responseObject) {
        NSLog(@"%@",responseObject);
        NetworkModel *network = [NetworkModel mj_objectWithKeyValues:responseObject];
        NSInteger code = network.code.integerValue;
        if (code == 0){
            self.likeCountLabel.text = [NSString stringWithFormat:@"%@人点赞", network.dataObject];
        }else{
            [CustomProgressHUD showErrorWithStatus:network.errorDescription];
        }
    } failure:^(id error) {
        [CustomProgressHUD showErrorWithStatus:k_NetWorkStatus_Error];
    }];
    
}

-(void)dealloc{
    NSLog(@"销毁了");
}

@end

